// pages/applyConsultation/applyConsultation.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    consultation_id: null,
    content: null,
    payType: "payConsultation",

    /**
     * payDialog
     */
    passWord: '',
    passWordArr: [],
    /**
     * Dialog
     */
    showPayDialog: false,
    showPwdErrorDialog: false,
    showPaySuccessDialog: false,
    showNoPasswordDialog: false,
    showInsufficientDialog: false,
    /**
     * tips
     */
    tipsInfo: {
      tipsType: null,
      showTopTips: false,
      errorMsg: null,
    },
  },
  /**
   * 表单提交
   */
  formSubmit: function (e) {
    var that = this;
    //console.log(e.detail.value);
    if (e.detail.value.content == "" || e.detail.value.content == null) {
      console.log(e.detail.value.content);
      that.showTopTips('请填写完整', 'warn');
      return;
    }
    // 支付一个新的咨询
    if (that.data.payType == 'payConsultation') {
      wx.request({
        url: app.globalData.server + '/consultation/',
        method: 'POST',
        data: {
          content: e.detail.value.content
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded', // 默认值
          'Authorization': 'Bearer ' + app.globalData.token,
          'x-wxapp-signature': null,
          'x-wxapp-timestamp': app.getTimestamp(),
          'x-wxapp-nonce': app.getRandom()
        },
        success: function (res) {
          console.log(res);
          if (res.data.message == 'Insufficient valid score') {
            that.showTopTips('可用积分不足，请充值', 'warn');
          } else if (res.data.message == 'ok') {
            that.setData({
              consultation_id: res.data.data.consultation_id
            });
            that.showPayDialog();
          }
        }
      })
    } else {
      that.showPayDialog();
    }
  },
  /**
   * tips提示 
   */
  showTopTips: function (msg, Type) {
    var that = this;

    that.setData({
      'tipsInfo.showTopTips': true,
      'tipsInfo.errorMsg': msg,
      'tipsInfo.tipsType': Type
    });
    setTimeout(function () {
      that.setData({
        'tipsInfo.showTopTips': false
      });
    }, 3000);
  },
  /** 
 * PayDialog相关
 */
  showPayDialog: function (e) {
    let that = this;
    that.setData({
      showPayDialog: !this.data.showPayDialog,
      //fixed: true,
    });
  },
  hidePayDialog: function () {
    this.setData({
      showPayDialog: !this.data.showPayDialog,
      passWord: '',
      passWordArr: [],
      fixed: false,
    });
  },
  /**
   * PwdErrorDialog相关
   */
  showPwdErrorDialog: function () {
    var that = this;
    that.setData({
      showPwdErrorDialog: true,
      //fixed: true,
    })
  },
  hidePwdErrorDialog: function () {
    var that = this;
    that.setData({
      showPwdErrorDialog: !this.data.showPwdErrorDialog,
      passWord: '',
      passWordArr: [],
    });
  },
  giveUpPay: function () {
    var that = this;
    that.hidePwdErrorDialog();
    that.hidePayDialog();
    wx.reLaunch({
      url: '../companyCenter/companyCenter',
    })
  },
  /**
   * PaySuccessDialog
   */
  showPaySuccessDialog: function () {
    var that = this;
    that.setData({
      showPaySuccessDialog: true,
      fixed: true,
    })
  },
  hidePaySuccessDialog: function () {
    var that = this;
    this.setData({
      showPaySuccessDialog: false,
      fixed: false,
    });
    wx.reLaunch({
      url: '../companyCenter/companyCenter',
    })
  },
  /**
   * NoPasswordDialog
   */
  showNoPasswordDialog: function () {
    var that = this;
    that.setData({
      showNoPasswordDialog: true,
      fixed: true,
    })
  },
  hideNoPasswordDialog: function () {
    var that = this;
    this.setData({
      showNoPasswordDialog: false,
      fixed: false,
    });
    wx.reLaunch({
      url: '../init/init',
    })
  },
  /**
   * InsufficientDialog
   */
  showInsufficientDialog: function () {
    var that = this;
    that.setData({
      showInsufficientDialog: true,
      fixed: true,
    })
  },
  hideInsufficientDialog: function () {
    var that = this;
    this.setData({
      showInsufficientDialog: false,
      fixed: false,
    });
    wx.redirectTo({
      url: '../init/init',
    })
  },

  /** 
   * 支付相关 
   */
  onChangeInput: function (e) {
    let that = this;
    var pwdArr = that.data.passWordArr;


    if (e.detail.value.length > that.data.passWord.length) {
      pwdArr.push(true);
    } else if (e.detail.value.length < that.data.passWord.length) {
      pwdArr.pop();
    }
    that.setData({
      passWordArr: pwdArr,
      passWord: e.detail.value,
    })
    // 密码输入完成
    console.log(e.detail.value);
    if (e.detail.value.length == 6) {

      that.pay();

    }
  },
  reInput: function () {
    var that = this;
    that.setData({
      showPayDialog: true,
    })
  },
  pay: function () {
    var that = this;

    wx.request({
      url: app.globalData.server + '/consultation/?consultation_id=' + that.data.consultation_id + '&passwd=' + that.data.passWord,
      method: 'PUT',
      data: {
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded', // 默认值
        'Authorization': 'Bearer ' + app.globalData.token,
        'x-wxapp-signature': null,
        'x-wxapp-timestamp': app.getTimestamp(),
        'x-wxapp-nonce': app.getRandom()
      },
      success: function (res) {
        console.log(res);
        if (res.data.message == "passwd wrong") {
          that.showPwdErrorDialog();
        } else if (res.data.message == "ok") {
          that.showPaySuccessDialog();
        } else if (res.data.message == "passwd not set") {
          that.showNoPasswordDialog();
        } else if (res.data.message == "Insufficient valid score") {
          that.showInsufficientDialog();
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;

    if (options.consultation_id != null) {
      that.setData({
        consultation_id: options.consultation_id,
        content: options.content,
        payType: 'payUnpaidConsultation',
      })
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})